package com.topfinance.cnaps2.web.sys;

import java.util.HashMap;
import java.util.List;

import com.topfinance.cnaps2.dto.SysOrgDto;
import com.topfinance.cnaps2.ebo.SysOrgEbo;
import com.topfinance.cnaps2.service.ISysOrgManager;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class SysOrgAction extends BaseSupport implements ModelDriven<SysOrgDto> {

	private ISysOrgManager orgManager = null;

	private SysOrgDto orgDto = new SysOrgDto();

	public SysOrgDto getModel() {
		return orgDto;
	}

	public void setSysOrgManager(ISysOrgManager orgManager) {
		this.orgManager = orgManager;
	}

	public SysOrgAction() {
		super();
	}

	public String execute() {
		return list();
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(orgDto));
			orgManager.addOrgDto(orgDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + orgDto.getOID();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}
	
	public String update() {
		try {
			log.debug(StringUtil.mapObject(orgDto));
			orgManager.editOrg(orgDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			String delId = request.getParameter("id");
			orgManager.deleteOrg(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}
	
	public String list() {
		log.debug("SysOrgAction-list begin");
		try {
			List l = orgManager.getAllOrgList();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysOrgAction-list end");
		return SUCCESS;
	}

	public String listTree() {
		log.debug("SysOrgAction-listTree begin");
		try {
			List l = orgManager.getOrgTree();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysOrgAction-listTree end");
		return SUCCESS;
	}

	public String test() {
		log.debug("SysFuncAction-test begin");
		try {
			SysOrgEbo oEbo1 = new SysOrgEbo();
			oEbo1.setName("测试部门1");
			oEbo1.setCode("T1001");
			oEbo1.setDesc("测试部门2");
			orgManager.save(oEbo1);

			SysOrgDto oEbo2 = new SysOrgDto();
			oEbo2.setName("测试部门2");
			oEbo2.setCode("T1002");
			oEbo2.setDesc("测试部门2");
			oEbo2.setPID(oEbo1.getOID());
			orgManager.addOrgDto(oEbo2);

			rtnResultMap = new HashMap();
			rtnResultMap.put("info", getText("create.success", "Test Success!"));
			return SUCCESS;
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysFuncAction-list end");
		return SUCCESS;
	}
	
}
